package com.dragon.lib_log.log

import com.dragon.lib_log.LoggerLevel
import com.dragon.lib_log.LoggerStrategy
import com.dragon.lib_log.log.level.FileLogger
import com.dragon.lib_log.log.level.LogCatLogger

/***
 * @ClassName Logger
 * @Author Administrator
 * @description 日志管理类
 * @date
 * @Version V1.0
 */
class LoggerManger private constructor(
    val TAG: String,
    val level: LoggerLevel,
    val strategy: LoggerStrategy,
    val isDebug: Boolean,
    val saveUrl: String,
) {
    private var logger: ILogger

    init {
        logger = getLogger()
    }

    constructor(builder: Builder) : this(
        builder.TAG,
        builder.level,
        builder.strategy,
        builder.isDebug,
        builder.saveUrl,
    )

    /**
     * 输出Debug
     */
    fun d(tag: String, log: String) {
        logger.d(tag, log)
    }

    /**
     * 输出Warn
     */
    fun w(tag: String, log: String) {
        logger.w(tag, log)
    }

    /**
     * 输出Verbose
     */
    fun v(tag: String, log: String) {
        logger.v(tag, log)
    }

    /**
     * 输出Error
     */
    fun e(tag: String, log: String) {
        logger.e(tag, log)
    }

    /**
     * 输出Info
     */
    fun i(tag: String, log: String) {
        logger.i(tag, log)
    }

    /*
    * 创建
    * 策略模式
    * */
    fun getLogger(): ILogger {
        when (strategy) {
            LoggerStrategy.LOGCAT -> {
                logger = LogCatLogger()
            }

            LoggerStrategy.FILE -> {
                logger = FileLogger()
            }

            else -> {}
        }
        logger.setLoggerLevel(level)
        logger.setDebug(isDebug)
        logger.setSaveUrl(saveUrl)
        logger.setLogTAG(TAG)
        return logger
    }

    /*
    * 构建者模式
    * */
    class Builder {
        var TAG: String = "IVideoLogger"
        var level: LoggerLevel = LoggerLevel.DEBUG
        var strategy: LoggerStrategy = LoggerStrategy.LOGCAT
        var isDebug: Boolean = true
        var saveUrl: String = ""

        fun setTAG(TAG: String): Builder {
            this.TAG = TAG
            return this
        }

        fun setLoggerLevel(level: LoggerLevel): Builder {
            this.level = level
            return this
        }

        /*
        * 策略模式
        * */
        fun setLoggerStrategy(strategy: LoggerStrategy): Builder {
            this.strategy = strategy
            return this
        }

        fun setDebug(isDebug: Boolean): Builder {
            this.isDebug = isDebug
            return this
        }

        fun setSaveUrl(url: String): Builder {
            this.saveUrl = url
            return this
        }

        fun build(): LoggerManger {
            return LoggerManger(this)
        }
    }
}